macro 'grain size map by pixel area          [A]';
var  
  i,j,xx,yy,dummy,n,mean,mode,min,max:integer;
  number,width,height,maxsize,maxarea,area:integer;

begin

PutMessage('*** Grain size by cross sectional area *** Use INVERSE grain boundary map = area map with black areas and white grain boundaries)');

SetScale(0,'pixel');
ResetCounter;
SetCounter(0);
SetOptions('X-Y center, Area');
SetParticleSize(1,99999);
AnalyzeParticles('reset');
number:=rCount;

UpdatesOff;

maxarea:=0;

for i:=1 to number do begin
if rArea[i] > maxarea then maxarea:=rArea[i];
end;

PutMessage('Select cut-off value for color coding. - NOTE: Image will be calibrated accordingly.');
maxarea:= GetNumber('max.area ? measured max (sq px) = ',maxarea);

 SetDensitySlice(1,250);
 MakeBinary; 

for i:= 1 to number do begin

xx:=Xi[i];
yy:=Yi[i];
AutoOutline(xx,yy);

dummy:= 254*rArea[i]/maxarea;
if dummy > 254 then dummy:=254;
SetForegroundColor(dummy);
if GetPixel(xx,yy) > 0 then Fill;

end;
Calibrate('straight','sq px',1,0,254,maxarea);
SetPalette('rainbow',0);
SelectAll;

end;


macro 'grain size map by pixel radius       [B]';
var
  i,j,xx,yy,dummy,n,mean,mode,min,max:integer;
  number,width,height,maxsize,maxarea,area:integer;
  r,maxr: real;

begin

PutMessage('*** Grain size by area-equivalent radius *** Use INVERSE grain boundary map = area map with black areas and white grain boundaries)');

SetScale(0,'pixel');
ResetCounter;
SetCounter(0);
SetOptions('X-Y center, Area');
SetParticleSize(1,99999);
AnalyzeParticles('reset');
number:=rCount;

UpdatesOff;

maxarea:=0;

for i:=1 to number do begin
if rArea[i] > maxarea then maxarea:=rArea[i];
end;

maxr:=sqrt(maxarea/3.14159);

PutMessage('Select cut-off value for color coding. - NOTE: Image will be calibrated accordingly.');
maxr:= GetNumber('max. radius ? measured max (px) = ',maxr);

 SetDensitySlice(1,250);
 MakeBinary; 

for i:= 1 to number do begin

xx:=Xi[i];
yy:=Yi[i];
AutoOutline(xx,yy);

dummy:= 254*sqrt(rArea[i]/3.14159)/maxr;
if dummy > 254 then dummy:=254;
SetForegroundColor(dummy);
if GetPixel(xx,yy) > 0 then Fill;

end;

Calibrate('straight','px',1,0.00,254,maxr);
SetPalette('rainbow',0);
SelectAll;

end;


macro 'grain size map by long diameter    [C]';
var
  i,j,xx,yy,dummy,n,mean,mode,min,max:integer;
  number,width,height,maxsize,maxarea,area:integer;
  d, maxd: real;

begin

PutMessage('*** Grain size by longest diameter *** Use INVERSE grain boundary map = area map with black areas and white grain boundaries)');

SetScale(0,'pixel');
ResetCounter;
SetCounter(0);
SetOptions('X-Y center, Major');
SetParticleSize(1,99999);
AnalyzeParticles('reset');
number:=rCount;

maxd:=0;

UpdatesOff;

for i:=1 to number do begin
if rMajor[i] > maxd then maxd:=rMajor[i];
end;

PutMessage('Select cut-off value for color coding. - NOTE: Image will be calibrated accordingly.');
maxd:= GetNumber('max. diameter ? measured max (px) = ',maxd);

 SetDensitySlice(1,250);
 MakeBinary; 

for i:= 1 to number do begin

xx:=Xi[i];
yy:=Yi[i];
AutoOutline(xx,yy);

dummy:= 254*rMajor[i]/maxd;
if dummy > 254 then dummy:=254;
SetForegroundColor(dummy);
if GetPixel(xx,yy) > 0 then Fill;

end;

Calibrate('straight','px',1,0.00,254,maxd);
SetPalette('rainbow',0);
SelectAll;

end;


macro 'grain shape map - axial ratio         [D]';
var
  i,j,xx,yy,dummy,n,mean,mode,min,max:integer;
  number,width,height,maxsize,maxarea,area:integer;
  ba,maxba: real;

begin

PutMessage('*** Axial ratio = short/long axis *** Use INVERSE grain boundary map = area map with black areas and white grain boundaries)');

SetScale(0,'pixel');
ResetCounter;
SetCounter(0);
SetOptions('X-Y center, Minor, Major, rUSer1');
SetParticleSize(1,99999);
AnalyzeParticles('reset');
number:=rCount;

UpdatesOff;

maxba:=0.000;

for i:=1 to number do begin
ba:=rMinor[i]/rMajor[i];
rUser1[i]:=ba;
if ba > maxba then maxba:= ba;
end;

PutMessage('Select cut-off value for color coding. - NOTE: Image will be calibrated accordingly.');
maxba:= GetNumber('max. axial ratio ? measured max = ',maxba);

 SetDensitySlice(1,250);
 MakeBinary; 

for i:= 1 to number do begin

xx:=Xi[i];
yy:=Yi[i];
AutoOutline(xx,yy);

dummy:= 254*rUser1[i]/maxba;
if dummy > 254 then dummy:=254;
SetForegroundColor(dummy);
if GetPixel(xx,yy) > 0 then Fill;

end;

Calibrate('straight',' ',1,0.00,254,maxba);
SetPalette('rainbow',0);
SelectAll;

end;


macro 'grain shape map - aspect ratio      [E]';
var
  i,j,xx,yy,dummy,n,mean,mode,min,max:integer;
  number,width,height,maxsize,maxarea,area:integer;
  ab,maxab: real;

begin

PutMessage('*** Aspect ratio = long/short axis *** Use INVERSE grain boundary map = area map with black areas and white grain boundaries)');

SetScale(0,'pixel');
ResetCounter;
SetCounter(0);
SetOptions('X-Y center, Minor, Major');
SetParticleSize(1,99999);
AnalyzeParticles('reset');
number:=rCount;

UpdatesOff;

maxab:=0.000;

for i:=1 to number do begin
ab:=rMajor[i]/rMinor[i];
rUser1[i]:= ab;
if ab > maxab then maxab:= ab;
end;

PutMessage('Select cut-off value for color coding. - NOTE: Image will be calibrated accordingly.');
maxab:= GetNumber('max. aspect ratio (min = 1.00) ? measured max = ',maxab);

 SetDensitySlice(1,250);
 MakeBinary; 


for i:= 1 to number do begin

xx:=Xi[i];
yy:=Yi[i];
AutoOutline(xx,yy);

dummy:= 254*(rUser1[i]-1.00)/(maxab-1.00);
if dummy > 254 then dummy:=254;
if dummy < 1 then dummy:=1;
SetForegroundColor(dummy);
if GetPixel(xx,yy) > 0 then Fill;

end;

Calibrate('straight',' ',1,1.00,254,maxab);
SetPalette('rainbow',0);
SelectAll;

end;


macro 'grain shape map - shape factor 1  [F]';
var
  i,j,xx,yy,dummy,n,mean,mode,min,max:integer;
  number,width,height,maxsize,maxarea,area:integer;
  sf1,maxsf1: real;

begin

PutMessage('*** Shape factor 1 = perimeter/area-equivalent perimeter *** Use INVERSE grain boundary map = area map with black areas and white grain boundaries)');

SetScale(0,'pixel');
ResetCounter;
SetCounter(0);
SetOptions('X-Y center, Area, Perimeter, rUser1');
SetParticleSize(1,99999);
AnalyzeParticles('reset');
number:=rCount;

UpdatesOff;

maxsf1:=0.000;

for i:=1 to number do begin
sf1:=rLength[i]/(2*sqrt(rArea[i]*3.14159));
rUser1[i]:=sf1;
if sf1 > maxsf1 then maxsf1:= sf1;
end;

PutMessage('Select cut-off value for color coding. - NOTE: Image will be calibrated accordingly.');
maxsf1:= GetNumber('max. of P/Pequ (min=1.00) ? measured max = ',maxsf1);

 SetDensitySlice(1,250);
 MakeBinary; 

for i:= 1 to number do begin

xx:=Xi[i];
yy:=Yi[i];
AutoOutline(xx,yy);

dummy:= 254*(rUser1[i]-1.00)/(maxsf1-1.00);
if dummy > 254 then dummy:=254;
if dummy < 1 then dummy:=1;
SetForegroundColor(dummy);
if GetPixel(xx,yy) > 0 then Fill;

end;

Calibrate('straight',' ',1,1.00,254,maxsf1);
SetPalette('rainbow',0);
SelectAll;

end;


macro 'grain shape map - shape factor 2  [G]';
var
  i,j,xx,yy,dummy,n,mean,mode,min,max:integer;
  number,width,height,maxsize,maxarea,area:integer;
  sf2,maxsf2: real;

begin

PutMessage('*** Shape factor 2 = area/perimeter-equivalent area *** Use INVERSE grain boundary map = area map with black areas and white grain boundaries)');

SetScale(0,'pixel');
ResetCounter;
SetCounter(0);
SetOptions('X-Y center, Area, Length, User1');
SetParticleSize(1,99999);
AnalyzeParticles('reset');
number:=rCount;

UpdatesOff;

maxsf2:=0.000;

for i:=1 to number do begin
sf2:=4*3.14159*rArea[i]/(rLength[i]*rLength[i]);
if sf2 > maxsf2 then maxsf2:= sf2;
rUser1[i]:=sf2;
end;

if maxsf2 > 1.00 then maxsf2:= 1.00;

PutMessage('Select cut-off value for color coding. - NOTE: Image will be calibrated accordingly.');
maxsf2:= GetNumber('max. of A/Aequ ? measured max = ',maxsf2);

 SetDensitySlice(1,250);
 MakeBinary; 

for i:= 1 to number do begin

xx:=Xi[i];
yy:=Yi[i];
AutoOutline(xx,yy);

dummy:= 254*rUser1[i]/maxsf2;
if dummy > 254 then dummy:=254;
if dummy < 1 then dummy:=1;
SetForegroundColor(dummy);
if GetPixel(xx,yy) > 0 then Fill;

end;

Calibrate('straight',' ',1,0,254,maxsf2);
SetPalette('rainbow',0);
SelectAll;

end;



macro 'grain orientation map                    [H]';
var
  i,j,xx,yy,dummy,n,mean,mode,min,max:integer;
  number,width,height,maxsize,maxarea,area:integer;
  an,minan,maxan: real;

begin

PutMessage('*** Orientation of long axis of grains *** Use INVERSE grain boundary map = area map with black areas and white grain boundaries)');

SetScale(0,'pixel');
ResetCounter;
SetCounter(0);
SetOptions('X-Y Center, Angle');
SetParticleSize(1,99999);
AnalyzeParticles('reset');
number:=rCount;

UpdatesOff;

maxan:=-90.000;
minan:= 90.000;

for i:=1 to number do begin
an:=rAngle[i];
if an > maxan then maxan:=an;
if an < minan then minan:=an;
end;

PutMessage('Select cut-off values for color coding. - NOTE: Image will be calibrated accordingly.');
maxan:= GetNumber('max. angle (180) ? measured max = ',maxan);
minan:= GetNumber('min. angle (0) ? measured min = ',minan);

 SetDensitySlice(1,250);
 MakeBinary; 


for i:= 1 to number do begin

xx:=Xi[i];
yy:=Yi[i];
AutoOutline(xx,yy);

an:=(rAngle[i]-minan)/(maxan-minan);
dummy:= 254*an;
if dummy > 254 then dummy:=254;
if dummy < 1 then dummy:=1;
SetForegroundColor(dummy);
if GetPixel(xx,yy) > 0 then Fill;

end;

Calibrate('straight','deg',1,minan,254,maxan);
SetPalette('rainbow',0);
SelectAll;

end;


